<template>
  <div class="userManner">
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-row>
        <el-col :span="6" >
          <el-button type="primary" size="small" @click="addUser">新增用户</el-button>
        </el-col>
        <el-col style="text-align:right" :span="18" >
          <span>所属分组:</span>
          <el-select v-model="search.group" clearable size="small" placeholder="请选择">
            <el-option
              v-for="item in groupList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-input v-model="search.name" clearable class="ipt" placeholder="请输入要查询的用户名/姓名" size="small" ></el-input>
          <el-button type="primary" size="small" @click="doSearch">查询</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="tableCard" shadow="always" :body-style="{ padding: '20px' }">
      <el-table
        v-loading="loading"
        :data="userList"
        :header-cell-style="{background:'#FAFAFA'}"
        :fit="true"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="50"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="userName"
          label="用户名"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="姓名"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="groupName"
          label="所属分组"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="role"
          label="角色"
          width="250"
        >
        </el-table-column>
        <el-table-column align="center" prop="status" label="状态" width="105">
          <template slot-scope="scope">
            <el-switch
              class="tablescope"
              v-model="scope.row.status"
              @change="changeStatus(scope.row)"
              active-value="0"
              inactive-value="1"
              inactive-text="禁用"
              active-text="启用"
              active-color="#1890FF"
              inactive-color="#C0C0C0">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="goodStatus"
          label="操作"
          width="150"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="resetPass(scope.row)">重置密码</el-button>
            <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
            
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        :page-sizes="page.size"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import systemApi from "@/api/lejv/system"
import Base64 from "@/utils/base64.min.js"
import md5 from "blueimp-md5"
import _ from "lodash"; //引入lodash
export default {
  name: "userManner",
  data() {
    return {
      search:{
        group:"",
        name:""
      },
      userList:[],
      loading: false,
      groupList:[],
      page: {
        start: 1,
        limit: 10,
        total: 10,
        size: [10, 20, 30],
      },
    };
  },
  computed: {},
  created() { 
    // console.log(this.$store.state.app.groupList);
    if(this.$store.state.app.groupList.length == 0){
      this.getGroupList()
    }else{
      this.groupList = this.$store.state.app.groupList
    }
    this.page.total = this.userList.length
    this.getUserList()
  },
  mounted() {},
  methods: {
    //获取用户列表
    getUserList(){
      this.loading = true;
      let params = {
        pageSize: this.page.limit,
        pageNum: this.page.start,
        ...this.search
      }
      params = _.pickBy(params, (item) => item); //使用lodash进行过滤参数为空的搜索条件
      systemApi.getUserList(params)
      .then(res=>{
        // console.log(res);
        if(res.code == 0){
          this.loading = false;
          this.userList = res.data.users
          this.page.total = res.data.totalElements
        }
      })
      .catch(res=>{
        this.loading = false;
        // this.$message({
        //   type:"error",
        //   message:"获取数据失败"
        // })
      })
    },
    //获取分组列表
    getGroupList(){
      let params = {
        pageSize:999,
        pageNum:1
      }
      systemApi.getGroupList(params)
      .then(res=>{
        // console.log(res);
        if(res.code == 0){
          this.groupList = res.data.groups.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.$store.state.app.groupList = this.groupList
          // console.log(this.$store.state.app.groupList)
        }
      })
      .catch(res=>{
        // this.$message({
        //   type:"error",
        //   message:"获取分组数据失败"
        // })
      })
    },
    //新增用户
    addUser(){
      this.$router.push("/system/addUser")
    },
    //查询
    doSearch(){
      this.page.start = 1
      this.getUserList()
    },
    //编辑
    edit(id){
      this.$router.push({name:"EditUser",params:{id}})
    },
    //重置密码
    resetPass(val){
      // console.log(val);
      this.$confirm('是否重置该用户初始密码为手机号?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let item = {
          id:val.id,
          password: md5(val.phone)
        }
        systemApi.resetPassword(item)
        .then(res=>{
          if(res.code == 0){
            this.$message({
              type: 'success',
              message: '已重置该用户密码!'
            });
          }
        })
        .catch(res=>{
          this.$message({
            type: 'error',
            message: '重置失败!'
          }); 
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置!'
        });          
      });
    },
    //删除
    del(val){
      this.$confirm('确认删除该用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        systemApi.delUser({id:val.id,userName:val.userName})
        .then(res=>{
          if(res.code == 0){
            this.$message({
              type: 'success',
              message: '已删除该用户!'
            });
            this.getUserList()
          }
        })
        .catch(res=>{
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除!'
        });          
      });
    },
    //改变状态
    changeStatus(val){
      // console.log(val);
      this.loading = true
      systemApi.updateStatus({id:val.id,status: val.status,userName:val.userName})
      .then(res=>{
        if(res.code == 0){
          this.loading = false
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        }else{
          this.$message({
            type: 'warning',
            message: res.msg
          });
          this.loading = false
        }
        this.getUserList()
      })
      .catch(res=>{
        this.loading = false
        this.$message({
          type: 'error',
          message: '操作失败!'
        });
      })
    },
    //分页切换
    handleSizeChange(val) {
      this.page.limit = val;
      this.getUserList();
    },
    //分页
    handleCurrentChange(val) {
      this.page.start = val;
      this.getUserList();
    },
  }
};
</script>
<style lang="scss" >
body{
  padding-right: 0px !important;
  // overflow: overlay;
}
</style>
<style lang="scss">
 .tablescope{
   width: 105px;
  .el-switch__label--left {
    position: relative;
    left: 45px;
    color: #fff;
    z-index: -1111;
    margin-right: -1px;
  }
  .el-switch__core{
    width: 50px !important;
  }
  .el-switch__label--right {
    position: relative;
    right: 46px;
    color: #fff;
    z-index: -1111;
    margin-left: -1px;
  }
  .el-switch__label--right.is-active {
    z-index: 98;
    color: #fff !important;
    span{
      width: 30px;
      font-size: 12px;
    }
  }
  .el-switch__label--left.is-active {
    span{
      width: 30px;
      font-size: 12px;
    }
    z-index: 98;
    color: #F9F9F9 !important;
  }
}

</style>
<style scoped lang="scss">
.userManner {
  margin: 20px;
  span{
    font-size: 14px;
    margin-right: 5px;
  }
  .ipt{
    width: 220px;
    margin: 0 10px 0 30px;
  }
  .tableCard{
    margin-top: 20px;
  }
}
</style>
